<template>
    <main class="user">
        <c-card>
            <a-row class="operation-bar">
                <c-btn span="1" type="primary" title="新增" @click="add"></c-btn>
            </a-row>
            <a-table :rowKey="record=>Math.random()"
                     @change="tableChangeHandler"
                     :columns="columns"
                     class="custom"
                     :pagination="{total:tableTotal,pageSize:params.pageSize,current:params.pageNum}"
                     :data-source="tableData">
                <div style="display:inline-block" slot="action"></div>
            </a-table>
        </c-card>
        <a-modal v-model="visible.post" title="新增" width="800px" :destroyOnClose='true' :footer="false">
            <post :visible.sync="visible.post" :type='type' :editData='editData' @success="addSuccessHandler"/>
        </a-modal>
    </main>
</template>

<script lang="js">
    import {list,doctorDel} from '@/api/doctor';
    import post from "./post"
    import moment from "moment";

    export default {
        components: {
            post
        },
        data() {
            return {
                columns: [
                    {title: '医生编码', dataIndex: 'doctorCode'},
                    {title: '医生名称', dataIndex: 'doctorName'},
                    {title: '所属科室', dataIndex: 'depName'},
                    {title: '医生年龄', dataIndex: 'age'},
                    {title: '医生性别', dataIndex: 'sex'},
                    {title: '联系电话', dataIndex: 'tel'},
                    {
                        title: '操作',
                        key: 'action',
                        align:'center',
                        customRender:(text,row)=>{
                            return <p><a onClick={()=>this.setEditData(row)} style="margin-right:10px">编辑</a><a onClick={()=>this.removeData(row)} style="color:red">删除</a></p>
                        },
                    },
                ],
                visible: {
                    post: false
                },
                tableData: [],
                tableTotal: 0,
                params: {
                    pageSize: 5,
                    pageNum: 1
                },
                editData:{},
                type:'add'
            }
        },
        created() {
            this.getList();
        },
        methods: {
            async getList() {
                const params = {...this.params};
                console.log(params)
                const res = await list(params);
                if (res['data']) {
                    console.log(res['data']['list'])
                    this.tableData = res['data']['list'];
                    this.tableTotal = res['data']['total']
                }
            },
            tableChangeHandler(e) {
                this.params.pageNum = e.current;
                this.getList();
            },
            add() {
                this.type = 'add'
                this.visible.post = true
            },
            addSuccessHandler() {
                this.getList();
            },
            setEditData(row){
                this.editData = row;
                this.type = 'edit';
                this.visible.post = true;
            },
            removeData(row){
                console.log(row);
                this.$confirm({
                    title:'确认删除该数据？',
                    onOk:()=>{
                        console.log(123);
                        this.deleteData(row.dcTid);
                    }
                })
            },
            async deleteData(id){
                const res = await doctorDel({dcTid:id});
                console.log(res);
                if(res.code==0){
                    this.$message.success('删除成功');
                    this.getList()
                }else{
                    this.$message.error(res.msg)
                }
            }
        }
    }
</script>

<style scoped lang="stylus">

</style>
